package com.woniuxy.utils;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Author: linKe
 * @Date: 2022/1/11 14:14
 * @FileName: RabbitConnection
 * @version: 1.0
 * @Description: 描述
 */
public class RabbitConnection {

    private static ConnectionFactory connectionFactory;

    static {
        connectionFactory = new ConnectionFactory();
        // 增强编辑
        connectionFactory.setHost("127.0.0.1");
        // 发消息的通信端口 5672 （15672是页面管理监测端口）
        connectionFactory.setPort(5672);
        // 设置虚拟主机  类似redis服务器中一个具体的操作库  默认/
        connectionFactory.setVirtualHost("/");

        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");

    }
    public static Connection getConnection() throws IOException, TimeoutException {
        // 连接对象
        return connectionFactory.newConnection();
    }

    public static void close(Connection connection, Channel channel) throws IOException, TimeoutException {
        // 关闭资源
       channel.close();
       connection.close();
    }
}
